n, m = map(int, input().split())
poss = list(map(int, input().split()))
poss.sort()
max_distance = float('inf')
if n <= m:
    max_distance = poss[-1] - poss[0]
for i in range(n - m):
    max_distance = min(max_distance, poss[i + m - 1] - poss[i])
print(max_distance)
